<?php

Yii::import('zii.widgets.CPortlet');

class BannerWidget extends CPortlet
{
	public $title='';
	public $tagName='div';
	public $width='940';
	public $position='right';
	/**
	 * @var array the HTML attributes for the portlet container tag.
	 */
	public $htmlOptions=array('class'=>'');
	/**
	 * Initializes the widget.
	 * This renders the open tags needed by the portlet.
	 * It also renders the decoration, if any.
	 */
	public function init()
	{
		ob_start();
		ob_implicit_flush(false);
		ob_clean();
	}
	
	/**
	 * Renders the content of the portlet.
	 */
	public function run()
	{
		$this->renderContent();
		$content=ob_get_clean();
		if($this->hideOnEmpty && trim($content)==='')
			return;
		echo $content;
	}
	
	protected function renderContent()
	{
		$imgs = Banner::model()->findAll("banner_position = :banner_position", array(":banner_position" => $this->position));
		
		if ( $imgs !== null ){
			foreach($imgs as $key=> $img)
			{
				$img = $img->attributes;
				echo CHtml::openTag($this->tagName, $this->htmlOptions)."\n";
				$image =  Banner::material_image($img['banner_alt'], $img['banner_img'], $this->width);
				echo CHtml::link($image, $img['banner_url']);
				echo CHtml::closeTag($this->tagName);
				
			}
		}
	}
}